package com.example.demo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.PhoneLocationMapper;
import com.example.demo.mapper.PhoneNumberSegmentMapper;
import com.example.demo.model.PhoneLocation;
import com.example.demo.model.PhoneNumberSegment;
import com.example.demo.util.CommonThread;
import com.example.demo.util.JedisClientPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.List;

/**
 * *  Author:zlf
 * *  2020/10/23 10:46
 **/
@Service
public class InitService {

    @Autowired
    PhoneNumberSegmentMapper phoneNumberSegmentMapper;
    @Autowired
    PhoneLocationMapper phoneLocationMapper;

    private static int count = 0;
    private static int total = 0;

    @PostConstruct
    public void execSql() {
        System.out.println("=================启动执行======================");
        JedisPool jedisPool = JedisClientPool.getJedisPoolInstance();
        Jedis jedis = jedisPool.getResource();

        List<PhoneNumberSegment> list = phoneNumberSegmentMapper.selectList(new QueryWrapper<>());
//        list.forEach(c -> {
//            jedis.set("phone:location:num" + c.getNumberSegment(), JSON.toJSONString(c),
//                    "NX", "EX", 259200);
//        });
//        operateCSVfile("C:\\Users\\A\\Desktop\\开通油滴\\开通油滴1.csv", jedis);
//        operateCSVfile("C:\\Users\\A\\Desktop\\开通油滴\\开通油滴2.csv", jedis);
//        operateCSVfile("C:\\Users\\A\\Desktop\\开通油滴\\开通油滴3.csv", jedis);
//        operateCSVfile("C:\\Users\\A\\Desktop\\开通油滴\\开通油滴4.csv", jedis);
//        operateCSVfile("C:\\Users\\A\\Desktop\\开通油滴\\开通油滴5.csv", jedis);
//        operateCSVfile("C:\\Users\\A\\Desktop\\开通油滴\\开通油滴6.csv", jedis);
//        operateCSVfile("C:\\Users\\A\\Desktop\\开通油滴\\开通油滴7.csv", jedis);
        JedisClientPool.release(jedisPool, jedis);
        int a = 3;
    }

    private void operateCSVfile(String path, Jedis jedis) {
        File inFile = new File(path); // 读取的CSV文件
        String inString = "";
        try {
            BufferedReader reader = new BufferedReader(new FileReader(inFile));
            while ((inString = reader.readLine()) != null) {
                String userId = inString.split(",")[0];
                String phone = inString.split(",")[1];
                if (phone.length() != 11) {
                    continue;
                }
                total++;
                System.out.println("总数：" + total + " 已处理：" + count + "保存成功:" + phone);
                String location = jedis.get("phone:location:num" + phone.substring(0, 7));
                if (!StringUtils.isEmpty(location)) {
                    JSONObject jo = JSON.parseObject(location);
                    count++;
                    new CommonThread() {
                        @Override
                        public void run() {
                            save(jo.getString("city"), jo.getString("op").replace("\n", ""),
                                    phone, userId);
                        }
                    }.start();
                    System.out.println("总数：" + total + " 已处理：" + count + "保存成功:" + phone + "  " + path);
                }
            }
            reader.close();
        } catch (FileNotFoundException ex) {
            System.out.println("没找到文件！");
        } catch (IOException ex) {
            System.out.println("读写文件出错！");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void save(String city, String op, String phone, String userId) {
        PhoneLocation pl = new PhoneLocation();
        pl.setCity(city);
        pl.setOp(op);
        pl.setPhone(phone);
        pl.setUserId(userId);

        phoneLocationMapper.insert(pl);
    }
}
